﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Model;
using Resources;

namespace Wicresoft.RDW.Helper
{
    public class PerfectPictureOperator
    {

        public bool Upload(HttpPostedFile pic, string mapPath, PerfectPicture perfectPicture,out string errorstr)
        {
            InitDirectory(mapPath);
            if (!CheckFileExt(pic))
            {
                string extstr = string.Empty;
                string[] extList = ConfigurationManager.AppSettings["Picture"].Split(',');
                foreach (string extItem in extList)
                {
                    extstr = extstr + "\\" + extItem;
                }
                errorstr =" : "+ NameResource.SysSupportPictureExp + extstr;
                return false;
            }
            string ext = Path.GetExtension(pic.FileName).ToLower();
            string picName = Path.GetFileNameWithoutExtension(pic.FileName) + "-" + DateTime.Now.ToString("yyMMddHHmmss") + GetRandomNumber() + ext;
            string filePath = Path.Combine(mapPath, picName);
            perfectPicture.Path = "../UploadImages/" + picName;
            //if (!ValidatePerfectPicture(perfectPicture))
            //{
            //    errorstr = "";
            //    return false;
            //}
            PerfectPicturePresent.Create(perfectPicture);
            pic.SaveAs(filePath);
            errorstr = "";
            return true;
        }
        /// <summary>
        /// 获取精彩图片
        /// </summary>
        /// <param name="mapPath"></param>
        /// <returns></returns>
        public static List<PerfectPicture> GetFiles()
        {
            return PerfectPicturePresent.GetList();
        }
        //private bool ValidatePerfectPicture(PerfectPicture perfectPicture)
        //{
        //    if (string.IsNullOrEmpty(perfectPicture.Path) || string.IsNullOrEmpty(perfectPicture.Desc_CN)
        //        || string.IsNullOrEmpty(perfectPicture.Desc_EN) || perfectPicture.CreateBy == 0 || perfectPicture.CreatTime == null)
        //    {
        //        return false;
        //    }
        //    return true;
        //}
        private void InitDirectory(string mapPath)
        {
            if (!Directory.Exists(mapPath))
            {
                Directory.CreateDirectory(mapPath);
            }
            

        }
        private bool CheckFileExt(HttpPostedFile file)
        {
            string ext = Path.GetExtension(file.FileName).ToLower().Replace(".", "");
            string[] extList = ConfigurationManager.AppSettings["Picture"].Split(',');
            foreach (string extItem in extList)
            {
                if (ext == extItem)
                {
                    return true;
                }
            }
            return false;
        }
        private int GetRandomNumber()
        {
            Random random = new Random();
            return (10000 + random.Next(1, 90000));
        }
    }
}